package com.kattomy.ssh.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.kattomy.ssh.dao.BaseDao;
import com.kattomy.ssh.dao.EmployeeDao;
import com.kattomy.ssh.entities.Employee;

public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {

	@Override
	public List<Employee> getAll() {
		String hql =  "FROM Employee e LEFT OUTER JOIN FETCH e.department";
		return getSession().createQuery(hql).list();
	}

	@Override
	public void delete(Integer id) {
		String hql = "DELETE FROM Employee e WHERE e.id = ?";
		getSession().createQuery(hql).setInteger(0, id).executeUpdate();
	}

	@Override
	public void saveOrUpdate(Employee employee) {
		getSession().saveOrUpdate(employee);
	}

	@Override
	public Employee get(Integer id) {
		return (Employee) getSession().get(Employee.class, id);
	}

	@Override
	public Employee queryEmployeeByLastName(String lastName) {
		String hql = "FROM Employee e WHERE e.lastName = ?";
		Employee employee = (Employee) getSession().createQuery(hql).setString(0, lastName).uniqueResult();
		System.out.println(employee);
		return employee;
	}
	
}
